package com.wengzw.message.common.utils;

import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 多线程下载日志记录
 *
 * @author wengzw
 * @date 2022/6/17 16:34
 */
public class LogThread implements Callable<Boolean> {

    public static AtomicLong LOCAL_FINISH_SIZE = new AtomicLong();
    public static AtomicLong DOWNLOAD_SIZE = new AtomicLong();
    public static AtomicLong DOWNLOAD_FINISH_THREAD = new AtomicLong();
    private long httpFileContentLength;
    // 下载线程数量
    public static int DOWNLOAD_THREAD_NUM = 5;

    public LogThread(long httpFileContentLength) {
        this.httpFileContentLength = httpFileContentLength;
    }

    @Override
    public Boolean call() throws Exception {
//        int[] downSizeArr = new int[5];
//        int i = 0;
//        double size = 0;
//        double mb = 1024d * 1024d;
//        // 文件总大小
//        String httpFileSize = String.format("%.2f", httpFileContentLength / mb);
//        while (DOWNLOAD_FINISH_THREAD.get() != DOWNLOAD_THREAD_NUM) {
//            double downloadSize = DOWNLOAD_SIZE.get();
//            downSizeArr[++i % 5] = Double.valueOf(downloadSize - size).intValue();
//            size = downloadSize;
//
//            // 每秒速度
//            double fiveSecDownloadSize = Arrays.stream(downSizeArr).sum();
//            int speed = (int)((fiveSecDownloadSize / 1024d) / (i < 5d ? i : 5d));
//
//            // 剩余时间
//            double surplusSize = httpFileContentLength - downloadSize - LOCAL_FINISH_SIZE.get();
//            String surplusTime = String.format("%.1f", surplusSize / 1024d / speed);
//            if (surplusTime.equals("Infinity")) {
//                surplusTime = "-";
//            }
//
//            // 已下大小
//            String currentFileSize = String.format("%.2f", downloadSize / mb + LOCAL_FINISH_SIZE.get() / mb);
//            String speedLog = String.format("> 已下载 %smb/%smb,速度 %skb/s,剩余时间 %ss", currentFileSize, httpFileSize, speed, surplusTime);
//            System.out.print("\r");
//            System.out.print(speedLog);
//            Thread.sleep(1000);
//        }
//        System.out.println();
//        return true;
        return null;
    }
}
